﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.ServiceBus.Messaging;
using System.Threading;
using System.Data;
using System.Data.SqlClient;
using System.Timers;

namespace WebReadDevice
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ReadData();
            }
            

        }


        SqlConnection conn = new SqlConnection("server=42.159.144.118;uid=sa;pwd=123.Tage;database=iotdb");
        
        
        private void ReadData()
        {
            string selectstr1 = "select sum(pcount) d0all from [iotdevice] where device = 'device0' ";
            string selectstr2 = "select sum(pcount) d0all from [iotdevice] where device = 'device1' ";
            string selectstr3= "select sum(pcount) d0all from [iotdevice] where device = 'device2' ";
            string selectstr4 = String.Format("select sum(pcount) d0td from [iotdevice] where device = 'device0' and updatetime > '{0} 0:00:00' and updatetime < '{0} 23:59:59' ", DateTime.Now.Date.ToShortDateString());
            string selectstr5 = String.Format("select sum(pcount) d0td from [iotdevice] where device = 'device1' and updatetime > '{0} 0:00:00' and updatetime < '{0} 23:59:59' ", DateTime.Now.Date.ToShortDateString());
            string selectstr6 = String.Format("select sum(pcount) d0td from [iotdevice] where device = 'device2' and updatetime > '{0} 0:00:00' and updatetime < '{0} 23:59:59' ", DateTime.Now.Date.ToShortDateString());

            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();
            SqlCommand cmd = new SqlCommand(selectstr1 + selectstr2 + selectstr3 + selectstr4 + selectstr5 + selectstr6);
            cmd.Connection = conn;
            da.SelectCommand = cmd;
            da.Fill(ds);
            Label2.Text = ds.Tables[0].Rows[0][0].ToString();
            Label4.Text = ds.Tables[1].Rows[0][0].ToString();
            Label6.Text = ds.Tables[2].Rows[0][0].ToString();
            Label1.Text = ds.Tables[3].Rows[0][0].ToString();
            Label3.Text = ds.Tables[4].Rows[0][0].ToString();
            Label5.Text = ds.Tables[5].Rows[0][0].ToString();
            return;
        }
    }
}